package controller;

import model.BlogDao;
import model.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/deleteBlog")
public class BlogDeleteServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session=req.getSession(false);
        if (session==null){
            resp.setStatus(403);
            return;
        }
        User user= (User) session.getAttribute("user");
        if (user==null){
            resp.setStatus(403);
            return;
        }
        String blogId=req.getParameter("blogId");
        if (blogId==null||blogId==""){
            resp.setContentType("text/html; charset=utf8");
            resp.getWriter().write("要删除的文章不存在!!");
            return;
        }
        BlogDao blogDao=new BlogDao();
        blogDao.deleteBlogById(Integer.parseInt(blogId));
        resp.sendRedirect("blog_list.html");
    }
}
